//
//  UseViewCtr.m
//  Four_years
//
//  Created by lx on 15/11/6.
//  Copyright © 2015年 中软三班四年小组. All rights reserved.
//

#import "UseViewCtr.h"

@interface UseViewCtr ()
@property UITableView * tableView;
@property UIImageView * imageView;
@property UIView * actionView;
@property UIView * actionGrayView;
@property UITapGestureRecognizer * downTap;
@property NSIndexPath * indexPath;
@property NSArray * bmobArray;
@end

@implementation UseViewCtr

- (id)init{
    if (self = [super init]) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
    //初始化_tableView
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -35, WIDTH, HEIGHT+35) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    [self initActionView];
    [self initNavigation];
    _downTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(downActionView)];
    [self.view addGestureRecognizer:_downTap];
    
}

- (void)getBmob{
    NSString * str = [NSString stringWithFormat:@"invite%ld",(long)_indexPath.row];
    NSLog(@"indexPath.row = %ld",(long)_indexPath.row);
    BmobQuery *bquery = [BmobQuery queryWithClassName:str];
    
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error) {
            NSLog(@"%@",error);
        }
        else {
            //赋值
            _bmobArray = array;
            //刷新
            [_tableView reloadData];
            //无直跳转
            if (array.count == 0) {
                [self initAlartView];
            }
        }
    }];
    
}

//如果没有没有值就提示
- (void)initAlartView{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"暂无活动" message:@"是否要添加一个活动呢？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    UIAlertAction * sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        FindAddActivityCtr * favc = [[FindAddActivityCtr alloc]init];
        [self.navigationController pushViewController:favc animated:YES];
    }];
    [alert addAction:sure];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)initNavigation{
    _imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"add@2x"]];
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:_imageView];
    self.navigationItem.rightBarButtonItem = rightItem;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(work:)];
    [_imageView addGestureRecognizer:tap];
}
//navigation右边按钮事件
- (void)work:(id)select{
    if (_actionView.frame.origin.y == HEIGHT) {
        [self.view addSubview:_actionView];
        [UIView animateWithDuration:0.3 animations:^{
            _actionView.frame = CGRectMake(0, HEIGHT-205, WIDTH, 205);
            _downTap.enabled = YES;
        } completion:^(BOOL finished) {
            [self.view addSubview:_actionGrayView];
        }];
    }else{
        [self downActionView];
    }

}
//创建actionView
- (void)initActionView{
    _actionGrayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-205)];
    _actionGrayView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    _actionView = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT, WIDTH, 205)];
    _actionView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    //创建数组
    NSArray * actionArray = @[@"只看男生",@"只看女生",@"查看全部",@"取消"];
    for (int i = 0; i<4; i++) {
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, i*50, WIDTH, 49)];
        if (i==3) {
            button.frame = CGRectMake(0, 3*50+5, WIDTH, 49);
        }
        [button setTitle:actionArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor colorWithWhite:1 alpha:1]];
        [_actionView addSubview:button];
        [button addTarget:self action:@selector(chose:) forControlEvents:UIControlEventTouchUpInside];
    }
    [self.view addSubview:_actionView];
    [self.view bringSubviewToFront:_actionView];
}
//actionView选择事件   待添加
- (void)chose:(UIButton *)but{
    NSString * str = [but titleForState:UIControlStateNormal];
    NSLog(@"%@",str);
    if ([str isEqualToString:@"取消"]) {
        [self downActionView];
    }
}
//放下actionView
- (void)downActionView{
    [UIView animateWithDuration:0.5 animations:^{
        _actionView.frame = CGRectMake(0, HEIGHT, WIDTH, 205);
    }];
    [_actionView removeFromSuperview];
    [_actionGrayView removeFromSuperview];
    _downTap.enabled = NO;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _bmobArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]init];
        cell.frame = CGRectMake(50, 0, WIDTH-50,45);
        //NSDictionary * dic = _bmobArray[indexPath.row];
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, 45)];
        //label.text = [dic valueForKey:@"add"];
        [cell addSubview:label];
        
    }
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"32");
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    SingleValue * single = [SingleValue getSingle];
    Group * group = single.arr[single.indexPath.section];
    Public * public = group.FindArray[single.indexPath.row];
    _indexPath = single.indexPath;
    self.title = public.PublicName;
    self.tabBarController.tabBar.hidden = YES;
    //获取bmob
    [self getBmob];

}
@end
